﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace DeliverySolution.HOME.PUBLIC
{
    public partial class VerifyImage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Random randObj = new Random();
            int random = randObj.Next(1000, 9999);
            Response.ContentType = "image/jpeg";
            CreateImage(random.ToString()).Save(Response.OutputStream, ImageFormat.Jpeg);
            Session.Add(General.Constants.SessionVariables.RandomNumber, random);
        }

        private static Bitmap CreateImage(string imageText)
        {
            Random random = new Random();
            Bitmap bitmap = new Bitmap(80, 30, PixelFormat.Format32bppArgb);
            Graphics g = null;
            try
            {
                //g = Graphics.FromImage(bitmap);
                //SolidBrush brush = new SolidBrush(ColorTranslator.FromHtml("#F4FEFF"));
                //SolidBrush brushWrite = new SolidBrush(ColorTranslator.FromHtml("#878787"));
                //g.FillRectangle(brush, 0, 0, 35, 20);
                //Font font = new Font("Arial", 8);
                //PointF point = new PointF(5F, 5F);
                //g.DrawString(imageText, font, brushWrite, point);


                // Create a graphics object for drawing.
                g = Graphics.FromImage(bitmap);
                g.SmoothingMode = SmoothingMode.AntiAlias;
                Rectangle rect = new Rectangle(0, 0, 80, 30);

                // Fill in the background.
                HatchBrush hatchBrush = new HatchBrush(
                  HatchStyle.SmallConfetti,
                  Color.LightGray,
                  Color.White);
                g.FillRectangle(hatchBrush, rect);

                // Set up the text font.
                SizeF size;
                float fontSize = rect.Height + 1;
                Font font;

                // Adjust the font size until the text fits within the image.
                do
                {
                    fontSize--;
                    font = new Font(
                      "Century Schoolbook",
                      fontSize,
                      FontStyle.Bold);
                    size = g.MeasureString(imageText, font);
                } while (size.Width > rect.Width);

                // Set up the text format.
                StringFormat format = new StringFormat();
                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Center;

                // Create a path using the text and warp it randomly.
                GraphicsPath path = new GraphicsPath();
                path.AddString(
                  imageText,
                  font.FontFamily,
                  (int)font.Style,
                  font.Size, rect,
                  format);
                float v = 4F;

                PointF[] points =
            {
            new PointF(
              random.Next(rect.Width) / v,
              random.Next(rect.Height) / v),
            new PointF(
              rect.Width - random.Next(rect.Width) / v,
              random.Next(rect.Height) / v),
            new PointF(
              random.Next(rect.Width) / v,
              rect.Height - random.Next(rect.Height) / v),
            new PointF(
              rect.Width - random.Next(rect.Width) / v,
              rect.Height - random.Next(rect.Height) / v)
            };

                Matrix matrix = new Matrix();
                matrix.Translate(0F, 0F);
                path.Warp(points, rect, matrix, WarpMode.Perspective, 0F);

                // Draw the text.
                hatchBrush = new HatchBrush(
                  HatchStyle.LargeConfetti,
                  Color.LightGray,
                  Color.DarkGray);
                g.FillPath(hatchBrush, path);

                // Add some random noise.
                int m = Math.Max(rect.Width, rect.Height);
                for (int i = 0; i < (int)(rect.Width * rect.Height / 30F); i++)
                {
                    int x = random.Next(rect.Width);
                    int y = random.Next(rect.Height);
                    int w = random.Next(m / 50);
                    int h = random.Next(m / 50);
                    g.FillEllipse(hatchBrush, x, y, w, h);
                }

                // Clean up.

                font.Dispose();
                hatchBrush.Dispose();
            }
            catch (Exception ee)
            {
                Console.WriteLine(ee.Message.ToString());
            }
            finally
            {
                if (g != null)
                {
                    g.Dispose();
                }
            }
            return (bitmap);
        }
    }
}